<template>
  <div class="app-container">
    <el-form
      :model="queryParamsAll"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
        <el-form-item label="评审项目" prop="reviewProject">
          <el-input
            v-model="queryParamsAll.reviewProject"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="编码" prop="reviewCode">
          <el-input
            v-model="queryParamsAll.reviewCode"
            placeholder="请输入编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:basis:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:basis:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:basis:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:basis:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch"></right-toolbar>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="filterText"
            placeholder="请输入要素名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            class="filter-tree"
            :data="treeData"
            node-key="id"
            ref="tree"
            v-loading = "loading"
            :props="defaultProps"
            :filter-node-method="filterNode"
            @node-click="handelTreeChange"
          >
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-table
          :data="menuList"
          v-loading = "loadings"
          stripe
          style="width: 100%"
          @selection-change="handleSelectionChange"
          border

        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" type="index" width="50" />
          <el-table-column label="评审项目" align="left" min-width="330" :show-overflow-tooltip="true" sortable>
            <template slot-scope="scope">
              <a @click.prevent="handleCLick(scope.row)" style="color: #409eff">{{ scope.row.reviewProject }}</a>
            </template>
          </el-table-column>
          <el-table-column
            label="评审标准"
            align="left"
            prop="reviewStandard"
            min-width="330"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="标准分"
            align="left"
            prop="reviewStandardScore"
            min-width="150"
          >
          <template slot-scope="scope">
            <span>{{ scope.row.reviewStandardScore | toFixed }}</span>
          </template>
          </el-table-column>
          <el-table-column
            label="控制点"
            align="left"
            prop="isControlPoint"
          />
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParamsAll.pageNum"
          :limit.sync="queryParamsAll.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
  </div>
</template>
<script>
import {
  listBasis,
  getBasis,
  delBasis,
  listBasisAll,
  addBasis,
  updateBasis,
  queryTreeData,
} from "../../../api/system/review/basis";
import PeopleSelect from "../../../components/Selection/index.vue";
import DepartmentDialog from "../../../components/DepartmentDialog/index.vue";
export default {
  name: "Manger",
  dicts: ["sys_normal_disable", "sys_review_classify"],
  components: {
    PeopleSelect,
    DepartmentDialog,
  },
  data() {
    return {
      menuList: [],
      treeData: [],
      defaultProps: {
        children: "children",
        label: "reviewProject",
      },
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工器具管理表格数据
      basisList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      loading: false,
      loadings: false,
      queryParamsAll: {
        pageNum: 1,
        pageSize: 10,
        reviewClassify: undefined,
        reviewCode: undefined,
        reviewProject: undefined,
        reviewDate: undefined,
        reviewPerson: undefined,
        reviewPersonId: undefined,
        reviewObjInformation: undefined,
        reviewObjective: undefined,
        reviewStandard: undefined,
        reviewStandardScore: undefined,
        reviewOption: undefined,
        reviewBasic: undefined,
        reviewEndScore: undefined,
        reviewStatus: undefined,
        isControlPoint: undefined,
        remark: undefined,
        parentId: 0,
      },
      SORTOptions: [
        {
          label: "体系评审",
          value: "体系评审",
        },
        {
          label: "标准化评审",
          value: "标准化评审",
        },
        {
          label: "应急能力建设评估",
          value: "应急能力建设评估",
        },
        {
          label: "安全性评价",
          value: "安全性评价",
        },
        {
          label: "责任制落实",
          value: "责任制落实",
        },
        {
          label: "基建责任制落实",
          value: "基建责任制落实",
        },
        {
          label: "班组评审",
          value: "班组评审",
        },
        {
          label: "其他",
          value: "其他",
        },
        {
          label: "工作落实量化处理",
          value: "工作落实量化处理",
        },
      ],
      // 表单参数
      form: {},
      Showbox: false,
      peopleOpen: false,
      peopleOpen1: false,
      childArr: [],
      // 部门名称
      filterText: "",
      // 表单校验
      rules: {
        ID: [{ required: true, message: "主键id不能为空", trigger: "blur" }],
        refId: [{ required: true, message: "外键不能为空", trigger: "blur" }],
        projectId: [
          { required: true, message: "项目id不能为空", trigger: "blur" },
        ],
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        SORT: [{ required: true, message: "类别不能为空", trigger: "blur" }],
        batchCode: [
          { required: true, message: "批次编号不能为空", trigger: "blur" },
        ],
        toolName: [
          { required: true, message: "器具名称不能为空", trigger: "blur" },
        ],
        kindNumber: [
          { required: true, message: "种类序列号不能为空", trigger: "blur" },
        ],
        TYPE: [{ required: true, message: "型号不能为空", trigger: "change" }],
        FACTORY: [
          { required: true, message: "制造厂家不能为空", trigger: "blur" },
        ],
        custodyPerson: [
          { required: true, message: "保管负责人不能为空", trigger: "blur" },
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" },
        ],
        managePerson: [
          { required: true, message: "管理责任人不能为空", trigger: "blur" },
        ],
        RESULTS: [
          { required: true, message: "检验结果不能为空", trigger: "blur" },
        ],
        inspectDate: [
          { required: true, message: "检验日期不能为空", trigger: "blur" },
        ],
        nextInspectDate: [
          { required: true, message: "下次检验日期不能为空", trigger: "blur" },
        ],
        REMARK: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        FILE: [
          { required: true, message: "附件信息不能为空", trigger: "blur" },
        ],
      },
    };
  },
  filters: {
    toFixed(value){
      return parseFloat(value).toFixed(1).replace(/\.0+$/, '')
    }
  },
  created() {
    this.queryTreeDataList();
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  methods: {
    handleCLick(event){
      this.$router.push({
        path: "/review/basicdata/basicDataForm",
        query: { queryId: event.id, title: "查看评审基础数据", flowId: 3 , dataList: this.treeData },
      });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    async getList() {
      this.loadings = true;
      let ss = "";
      for(let a=0;a<this.childArr.length;a++){
        ss += this.childArr[a] + ","
      }
      ss.substring(0,ss.length -1);

      this.queryParamsAll.reviewEndScore = ss;
      await listBasisAll(this.queryParamsAll).then(async (response) => {
        this.total = response.total;
        this.menuList = response.data;
        //console.log(this.menuList)
        this.loadings = false;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.reviewProject.indexOf(value) !== -1;
    },
    handelTreeChange(val, index, s, data) {
      // this.queryParamsAll.reviewProject = "";
      // this.queryParamsAll.reviewCode = "";
      this.queryParamsAll.parentId = val.id;
      this.childArr = []
      this.getAllChild(val);
      this.queryParamsAll.pageNum = 1;
      this.queryParamsAll.reviewStandard = val.id;
      this.getList();
      this.queryParamsAll.reviewStandard = undefined;
    },
    getAllChild(obj){
      if (Object.prototype.hasOwnProperty.call(obj, 'children')) {
            for (const item of obj.children) {
                 if (Object.prototype.hasOwnProperty.call(item, 'children')) {
                      this.childArr.push(item.id)
                      this.getAllChild(item)
                 } else {
                      this.childArr.push(item.id)
                 }
            }
            this.getAllChild(obj.children)
      } else {
            return null
      }
    },
    open1() {
      this.peopleOpen = true;
    },
    open2() {
      this.peopleOpen1 = true;
    },
    //选择人的确定按钮事件 submitPeople(nikeNamelist)方法传参一个默认接收用户昵称数组   submitPeople(peopleList,nikeNamelist)方法传参两个则是接收用户昵称数组和用户账号数组
    submitPeople(peopleList) {
      this.peopleOpen = false;
    },
    submitPeople1(peopleList) {
      this.peopleOpen1 = false;
    },
    handleShow() {
      this.Showbox = !this.Showbox;
      const element = this.$refs.handle_box_content;
      if (this.Showbox) {
        element.style.overflow = "visible";
        element.style.height = "fit-content";
      } else {
        element.style.overflow = "hidden";
        element.style.height = "50px";
      }
    },
    /** 查询工器具管理列表 */
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ID: undefined,
        refId: undefined,
        projectId: undefined,
        projectName: undefined,
        SORT: undefined,
        batchCode: undefined,
        toolName: undefined,
        kindNumber: undefined,
        TYPE: undefined,
        FACTORY: undefined,
        custodyPerson: undefined,
        startTime: undefined,
        endTime: undefined,
        managePerson: undefined,
        RESULTS: undefined,
        inspectDate: undefined,
        nextInspectDate: undefined,
        REMARK: undefined,
        FILE: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParamsAll.parentId = 0;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {

      this.$router.push({
        path: "/review/basicdata/basicDataForm",
        query: { title: "新增评审基础数据", flowId: 2 , dataList: this.treeData ,IDS: this.queryParamsAll.parentId},
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ID = row.id || this.ids;
      getBasis(ID).then((response) => {});

      this.$router.push({
        path: "/review/basicdata/basicDataForm",
        query: { ID: ID, title: "修改评审基础数据", flowId: 1 ,dataList: this.treeData},
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const IDs = row.id || this.ids;
      this.$modal
        .confirm("确定删除吗？")
        .then(() => {
          return delBasis(IDs);
        })
        .then(() => {
          // this.getList();
          this.$modal.msgSuccess("删除成功");
          this.queryParamsAll.parentId = "0";
          this.queryTreeDataList();
        })
        .catch(() => {})
        .finally(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/basis/export",
        {
          ...this.queryParamsAll,
        },
        `basis_${new Date().getTime()}.xlsx`
      );
    },
    queryTreeDataList() {
      this.loading = true;
      queryTreeData().then((response) => {
        const map = {}; // 使用一个对象来存储每个节点
        const tree = []; // 最终的树形结构

        response.forEach((item) => {
          item.children = []; // 初始化 children 属性
          map[item.id] = item; // 使用 id 作为节点的唯一标识
        });

        response.forEach((item) => {
          const parent = map[item.parentId]; // 查找父节点
          if (parent) {
            parent.children.push(item); // 将当前节点添加为父节点的子节点
          } else {
            tree.push(item); // 没有父节点的节点将直接放入树的顶层
          }
        });
        this.loading = false;
        this.getList();
        return (this.treeData = tree);
      });
    },
  },
};
</script>
<style scoped lang="scss">
.handle_box_content {
  width: 81%;
  height: 50px;
  overflow: hidden;
}
.el-forms {
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
}
// .demo-table-expand {
//    font-size: 0;
//  }
//  .demo-table-expand label {
//    width: 90px;
//    color: #99a9bf;
//  }
//  .demo-table-expand .el-form-item {
//    margin-right: 0;
//    margin-bottom: 0;
//    width: 50%;
//  }
</style>
